package com.mydiceplayer.impls.engine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;

import com.mydiceplayer.impls.media.ImplProviderMediaVideo;
import com.mydiceplayer.interfaces.engine.EngineVideoDir;
import com.mydiceplayer.interfaces.media.ProviderMedia;
import com.mydiceplayer.models.ModelVideo;
import com.mydiceplayer.models.ModelVideoDir;

public class ImplEngineVideoDir implements EngineVideoDir{

	Context context;
	
	/**
	 * @param context
	 */
	public ImplEngineVideoDir(Context context) {
		super();
		this.context = context;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ModelVideoDir> getVideoDirList() {
		
		ProviderMedia providerMedia = new ImplProviderMediaVideo(context);
		List<ModelVideo> videos = (List<ModelVideo>) providerMedia.getList();
		return videos2videoDir(videos);
		
	}
	
	private List<ModelVideoDir> videos2videoDir(List<ModelVideo> videos){
		if(videos == null || videos.size() == 0){
			return null;
		}
		
		Map<String, ModelVideoDir> map = new HashMap<String, ModelVideoDir>();
		List<ModelVideoDir> videoDirs = new ArrayList<ModelVideoDir>();
		
		ModelVideoDir videoDir;
		String nameDir;
		for(ModelVideo video : videos){
			nameDir = video.getAlbum();
			if(map.containsKey(nameDir)){
				videoDir = map.get(nameDir);
			}else{
				videoDir = new ModelVideoDir();
				videoDir.setName(nameDir);
				videoDirs.add(videoDir);
				
				map.put(nameDir, videoDir);
			}
			
			
			videoDir.addVideos(video);
		}
		
		
		
		
		return videoDirs;
		
	}
	

}
